package com.zky.pet.service.impl;


import com.zky.basic.service.IBaiduAiAuditService;
import com.zky.pet.domain.Pet;
import com.zky.pet.domain.PetDetail;
import com.zky.pet.domain.PetOnlineAuditLog;
import com.zky.pet.domain.PetType;
import com.zky.pet.mapper.PetDetailMapper;
import com.zky.pet.mapper.PetMapper;
import com.zky.pet.mapper.PetOnlineAuditLogMapper;
import com.zky.pet.mapper.PetTypeMapper;
import com.zky.pet.service.IPetService;
import com.zky.basic.service.impl.BaseServiceImpl;
import com.zky.util.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * 业务实现类：
 */
@Service
public class PetServiceImpl extends BaseServiceImpl<Pet> implements IPetService {

    @Autowired
    private PetTypeMapper petTypeMapper;
    @Autowired
    private PetDetailMapper petDetailMapper;
    @Autowired
    private PetMapper petMapper;
    @Autowired
    private IBaiduAiAuditService baiduAiAuditService;
    @Autowired
    private PetOnlineAuditLogMapper petOnlineAuditLogMapper;
    @Override
    public List<PetType> getTypeTree() {
        List<PetType> petTypes = petTypeMapper.findAll();
        Map<Long, PetType> map = new HashMap<>();
        for (PetType petType : petTypes) {
            map.put(petType.getId(),petType);
        }
        List<PetType> typeTree = new ArrayList<>();
        for (PetType petType : petTypes) {
            if (petType.getParentId()==null){
                typeTree.add(petType);
            }else {
                PetType petType1 = map.get(petType.getParentId());
                petType1.getChildren().add(petType);
            }

        }

        return typeTree;
    }



    @Override
    public void delete(Long id) {
        //删除宠物详情
        petDetailMapper.belByPetId(id);

        super.delete(id);

    }

    @Override
    public void add(Pet pet) {

        super.add(pet);
        PetDetail petDetail = pet.getPetDetail();
        if (petDetail!=null){
            petDetail.setPetId(pet.getId());
            petDetailMapper.add(petDetail);
        }

    }

    @Override
    public void update(Pet pet) {
        super.update(pet);
        PetDetail petDetail = pet.getPetDetail();
        if (petDetail!=null){
            petDetail.setPetId(pet.getId());
            petDetailMapper.updateByPetId(petDetail);
        }
    }

    @Override
    public void patchDelete(List<Long> ids) {
        for (Long id : ids) {
            delete(id);

        }
    }



    @Override
    public void offsale(List<Long> ids) {

            petMapper.offSale(ids,new Date());

    }

    @Override
    public AjaxResult onsale(List<Long> ids) {
        for (Long id : ids) {
            Pet pet = petMapper.findOne(id);
            Boolean boo1 = baiduAiAuditService.textAudit(pet.getName());
            Boolean boo2 = baiduAiAuditService.imageAudit(pet.getResources());
            PetOnlineAuditLog log = new PetOnlineAuditLog();
            log.setPetId(id);
            if (boo1&&boo2){
                petMapper.onsale(id,new Date());
                //添加日志
                log.setState(1);
                log.setNote("审核成功");
                petOnlineAuditLogMapper.add(log);
            }else {
                log.setState(0);
                log.setNote("审核失败");
                petOnlineAuditLogMapper.add(log);
                return new AjaxResult(false,"审核失败");
            }
        }
        return new AjaxResult();
    }

}
